package com.itheima.web.response;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/resp")
public class ResponseDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      //1.获取字节输入流
        FileInputStream fis=new FileInputStream("C:\\Users\\Odyssey\\Pictures\\徐师傅\\xsf.jpg");

        //2.获取字符输出流
        ServletOutputStream outputStream = response.getOutputStream();

        //3.完成流的拷贝
        /*byte buff[] = new byte[1024];
        int len = 0;
        while((len=fis.read(buff))!=-1){
            outputStream.write(buff,0,len);
        }
        fis.close();*/

        //3 apache工具类实现copy
        IOUtils.copy(fis,outputStream);
        fis.close();

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}